﻿using Newtonsoft.Json;

namespace NetCore8Demo.Tool
{
    public class JsonParam
    {
        static string _paramPath = Directory.GetCurrentDirectory() + "\\Param";

        /// <summary>
        /// 保存Json参数
        /// </summary>
        /// <param name="fileName">保存的文件名</param>
        /// <param name="param">参数对象</param>
        /// <returns></returns>
        public static bool SaveParam<T>(string fileName, T param)
        {
            try
            {
                if (!Directory.Exists(_paramPath))
                {
                    Directory.CreateDirectory(_paramPath);
                }
                string json = JsonConvert.SerializeObject(param);

                //string json = JsonConvert.SerializeObject(param, Formatting.Indented, new JsonSerializerSettings
                //{
                //    ReferenceLoopHandling = ReferenceLoopHandling.Ignore, // 忽略循环引用  
                //    DefaultValueHandling = DefaultValueHandling.Ignore    // 忽略默认值  
                //});


                File.WriteAllText(_paramPath + "\\" + fileName +".json", json);
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }


        /// <summary>
        /// 保存Json参数
        /// </summary>
        /// <param name="path">保存的路径</param>
        /// <param name="fileName">保存的文件名</param>
        /// <param name="param">参数对象</param>
        /// <returns></returns>

        public static bool SaveParamPath(string path, string fileName, object param)
            {
                try
                {

                    if (!Directory.Exists(_paramPath + "\\" + path))
                    {
                        Directory.CreateDirectory(_paramPath + "\\" + path);
                    }

                    string json = JsonConvert.SerializeObject(param);
                    File.WriteAllText(_paramPath + "\\" + path + "\\" + fileName + ".json", json);
                    return true;
              
                }
                catch (Exception)
                {
                    return false;
                }


           

            }


        public static T? LoadParam<T>(string fileName)
        {
            try
            {
                if (File.Exists(_paramPath + "\\" + fileName + ".json"))
                {
                    string allText = File.ReadAllText(_paramPath + "\\" + fileName + ".json");
                    return JsonConvert.DeserializeObject<T>(allText);
                }
                else
                {
                    return default(T);//
                }

            }
            catch (Exception )
            {

                return default(T);
            }
         
        }

        public static T? LoadParamPath<T>(string path, string fileName)
        {

            try
            {
                if (File.Exists(_paramPath + "\\" + path+"\\"+fileName + ".json"))
                {

                    string allText = File.ReadAllText(_paramPath + "\\" + path + "\\" + fileName + ".json");

                    return JsonConvert.DeserializeObject<T>(allText);
                   
                }
                else
                {
                    return default(T);
                }

            }
            catch (Exception)
            {

                return default(T);
            }
           
        }


      

       
    }


   
}
